
export default class Methods {
    /**
     * 生成UUID, 默认长度为19
     * @param len
     * @returns {string}
     */
    newUuId(len = 19) {
        var timestamp = Math.floor(Date.now()); // 获取当前时间的时间戳（单位：毫秒）
        var randomNumStr = '' + timestamp; // 将时间戳转换为字符串类型

        if (randomNumStr.length < len) {
            while (randomNumStr.length < len) {
                randomNumStr += Math.floor(Math.random() * 10); // 在原有的时间戳基础上追加随机数
            }

            return randomNumStr;
        } else {
            throw new Error('The generated number is too long!');
        }
    }

    /**
     * 生成随机字符串, 默认长度为5
     * @param len
     * @returns {string}
     */
    randomString(len) {
        len = len || 5;
        var _chars =
            "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678oOLl9gqVvUuI1";
        var maxPos = _chars.length;
        var str = "";
        for (var i = 0; i < len; i++) {
            str += _chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return str;
    };

    /**
     * 获取url参数
     * @param name
     * @returns {string}
     */
    getUrlParam = (name) => {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    };

    // 字符串截取前6位
    getFirstSix = (str, length) => {
        if (str.length > length) {
            return str.substring(0, length);
        }
        return str;
    }

    // 防抖函数
    debounce = (fn, delay) => {
        let timer = null;
        return function (...args) {
            if (timer) clearTimeout(timer);
            timer = setTimeout(() => {
                fn.apply(args);
            }, delay);
        }
    }

    // 驼峰转下划线
    camelToUnderline = (str) => {
        return str.replace(/([A-Z])/g, "-").toLowerCase();
    }

}